const arr = [1,2,3,[4,5,[6,7,8,[9,10]]]]

const flat = (arr) => {
    const queue = [], res = []
    queue.push(arr)
    while(queue.length) {
        const top = queue.shift()
        let size = top instanceof Array ? top.length : 0
        if(!size) {
            res.push(top)
        }
        while(size > 0) {
            const val = top.shift()
            if(val instanceof Array) {
                queue.push(val)
            }
            else {
                res.push(val)
            }
            size--
        }
    }
    return res
}
console.log(flat(arr));